package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.UserDto;
import com.zzyl.entity.User;
import com.zzyl.entity.UserRole;
import com.zzyl.mapper.UserMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.UserService;
import com.zzyl.utils.NoProcessing;
import com.zzyl.vo.UserVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description UserServiceImpl
 * @Author xxxx
 * @Date 2024-10-24
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;
    private final UserRoleMapper userRoleMapper;

    /**
     * 用户添加
     * @param userDto
     */
    @Override
    public void addUser(UserDto userDto) {
        User user = BeanUtil.toBean(userDto, User.class);
        user.setPassword("123456");
        user.setDataState(SuperConstant.DATA_STATE_0);
        user.setUsername(user.getEmail());
        user.setNickName(user.getRealName());
        int flag = userMapper.insert(user);
        if (flag == 0) {
            throw new RuntimeException("保存用户信息出错");
        }
        //保存用户角色中间表
        List<UserRole> userRoles = userDto.getRoleVoIds().stream().map(r ->
                UserRole.builder()
                        .userId(user.getId())
                        .roleId(Long.valueOf(r))
                        .dataState(SuperConstant.DATA_STATE_0)
                        .build()
        ).collect(Collectors.toList());
        flag = userRoleMapper.batchInsert(userRoles);
        if (flag == 0) {
            throw new RuntimeException("保存用户角色中间表出错");
        }
    }

    /**
     * 用户分页
     * @param userDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<UserVo> findUserVoPage(UserDto userDto, int pageNum, int pageSize) {
        userDto.setDeptNo(NoProcessing.processString(userDto.getDeptNo()));
        PageHelper.startPage(pageNum, pageSize);
        Page<UserVo> userVoPage = userMapper.getByPage(userDto);
        //将page封装给PageResponse<UserVo>
        return PageResponse.of(userVoPage, UserVo.class);
    }

    /**
     * 用户修改
     * @param userDto
     * @return
     */
    @Override
    @Transactional
    public Boolean updateUser(UserDto userDto) {
        User user = BeanUtil.toBean(userDto, User.class);
        int flag = userMapper.updateByPrimaryKeySelective(user);
        if (flag == 0) {
            throw new RuntimeException("保存用户信息出错");
        }

        List<UserRole> userRole = userDto.getRoleVoIds().stream().map(r ->
                UserRole.builder()
                        .userId(user.id)
                        .roleId(Long.valueOf(r))
                        .dataState(SuperConstant.DATA_STATE_0)
                        .build()
        ).collect(Collectors.toList());
        userRoleMapper.deleteRoleByUserId(userDto.getId());
        flag = userRoleMapper.batchInsert(userRole);
        if (flag == 0){
            throw new RuntimeException("保存用户角色中间表出错");
        }
        return true;
    }

    /**
     * 启用或禁用用户
     * @param id
     * @param status
     */
    @Override
    public void startOrBan(Long id, String status) {
        userMapper.startOrBan(id,status);
    }

    /*    *//**
     * 用户列表
     * @param userDto
     * @return
     *//*
    @Override
    public ResponseResult userList(UserDto userDto) {
        User user = BeanUtil.toBean(userDto, User.class);
        userMapper.selectByPrimaryKey(user.getId());
        return null;
    }*/
}
